package com.hui.controller.user;


import com.google.common.collect.Lists;
import com.hui.entity.dao.SystemCity;
import com.hui.entity.vo.CityVo;
import com.hui.model.addr.AddressParam;
import com.hui.model.addr.IdParam;
import com.hui.service.SystemCityService;
import com.hui.service.UserAddressService;
import com.hui.util.ResponseResult;
import com.hui.util.module.CityTreeUtil;
import com.hui.util.module.GetUserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/address")
public class UserAddressController {

    private final UserAddressService userAddressService;
    private final SystemCityService systemCityService;

    @GetMapping("/city_list")
    @ApiOperation(value = "城市列表", notes = "城市列表")
    public ResponseResult<List<CityVo>> getTest() {
        List<SystemCity> systemCities = systemCityService.list();

        List<CityVo> cityVOS = Lists.newArrayList();

        for (SystemCity systemCity : systemCities) {
            CityVo cityVO = new CityVo();

            cityVO.setValue(systemCity.getCityId());
            cityVO.setLabel(systemCity.getName());
            cityVO.setPid(systemCity.getParentId());

            cityVOS.add(cityVO);
        }

        return ResponseResult.okResult(CityTreeUtil.list2TreeConverter(cityVOS, 0));

    }

    /**
     * 用户地址列表
     */
    @GetMapping("/list")
    public ResponseResult getcategory(@RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "limit", defaultValue = "10") int limit,
                                      HttpServletRequest request) {

        Long uid = GetUserInfo.getUid(request);
        return userAddressService.getList(uid, page, limit);
    }

    /**
     * 用户添加或修改地址
     */
    //TODO 不符合文档，要修改
    @PostMapping("/edit")
    public ResponseResult addUserAddress(@RequestBody AddressParam addressParam, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        return userAddressService.addAndEdit(uid, addressParam);
    }


    /**
     * 地址详情
     */
    //TODO 没有做地区选泽的点击回显
    @GetMapping("/detail")
    public ResponseResult addressDetail(String id) {
        log.info("传入的ID {}", id);
        return userAddressService.getDetail(Long.valueOf(id));
    }

    /**
     * 删除用户地址
     */
    @PostMapping("/del")
    @ApiOperation(value = "删除用户地址", notes = "删除用户地址")
    public ResponseResult deleteUserAddress(@Valid @RequestBody IdParam idParam) {
        userAddressService.removeById(Long.valueOf(idParam.getId()));
        return ResponseResult.okResult();
    }

    /**
     * 设置默认地址 //TODO 设置默认地址，感觉可以优化
     */
    @PostMapping("/address/default/set")
    @ApiOperation(value = "设置默认地址", notes = "设置默认地址")
    public ResponseResult<Boolean> setDefault(@Valid @RequestBody IdParam idParam, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
//        userAddressService.setDefault(uid, Long.valueOf(idParam.getId()));
        return ResponseResult.okResult();
    }

}
